/**********************************************************************
 * Filename: Equipa.java
 * Created: 2012/04/30
 * @author Fernando Brito e Abreu
 **********************************************************************/
package org.quasar.CopaPaises.businessLayer;

import java.util.Set;
import java.util.HashSet;

public class Equipa {
	public static Set<Equipa> allInstances = new HashSet<Equipa>();

	private Jogo jogo;
	private Pais seleccao;
	private int nSubstituicoes;

	/**********************************************************************
	 * Default constructor
	 **********************************************************************/
	public Equipa() {
		allInstances.add(this);
	}

	/**********************************************************************
	 * Parameterized constructor
	 * 
	 * @param jogo
	 *            the jogo to initialize
	 * @param seleccao
	 *            the seleccao to initialize
	 **********************************************************************/
	public Equipa(Jogo jogo, Pais seleccao) {
		this.jogo = jogo;
		this.seleccao = seleccao;
		nSubstituicoes = 3;
		allInstances.add(this);
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER getter for Equipa[*] <-> Jogo[1]
	 * 
	 * @return the jogo of the equipa
	 **********************************************************************/
	public Jogo jogo() {
		return jogo;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER setter for Equipa[*] <-> Jogo[1]
	 * 
	 * @param jogo
	 *            the jogo to set
	 **********************************************************************/
	public void setJogo(Jogo jogo) {
		this.jogo = jogo;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER getter for Equipa[*] <-> Pais[1]
	 * 
	 * @return the seleccao of the equipa
	 **********************************************************************/
	public Pais seleccao() {
		return seleccao;
	}

	/**********************************************************************
	 * ASSOCIATIVE2MEMBER setter for Equipa[*] <-> Pais[1]
	 * 
	 * @param seleccao
	 *            the seleccao to set
	 **********************************************************************/
	public void setSeleccao(Pais seleccao) {
		this.seleccao = seleccao;
	}

	/**********************************************************************
	 * MEMBER2ASSOCIATIVE getter for Equipa[*] <-> Membro[11..*]
	 * 
	 * @return the membro of the equipa
	 **********************************************************************/
	public Set<Membro> membro() {
		Set<Membro> result = new HashSet<Membro>();
		for (Membro x : Membro.allInstances)
			if (x.equipa() == this)
				result.add(x);
		return result;
	}

	/**********************************************************************
	 * MEMBER2ASSOCIATIVE setter for Equipa[*] <-> Membro[11..*]
	 * 
	 * @param membro
	 *            the membro to set
	 **********************************************************************/
	public void setMembro(Set<Membro> membro) {
		for (Membro x : membro)
			x.setEquipa(this);
	}

	/**********************************************************************
	 * MEMBER2MEMBER getter for Equipa[*] <-> Jogador[11..*]
	 * 
	 * @return the jogador of the equipa
	 **********************************************************************/
	public Set<Jogador> jogador() {
		Set<Jogador> result = new HashSet<Jogador>();
		for (Membro x : Membro.allInstances)
			if (x.equipa() == this)
				result.add(x.jogador());
		return result;
	}

	/**********************************************************************
	 * MEMBER2MEMBER setter for Equipa[*] <-> Jogador[11..*]
	 * 
	 * @param jogador
	 *            the jogador to set
	 **********************************************************************/
	public void setJogador(Set<Jogador> jogador) {
		for (Jogador t : jogador)
			for (Membro x : Membro.allInstances)
				if (x.equipa() == this)
					x.setJogador(t);
	}

	/**********************************************************************
	 * User-defined operation specified in SOIL/OCL
	 **********************************************************************/
	public void criaFichaJogoFalsa() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	/**********************************************************************
	 * Object serializer
	 **********************************************************************/
	public String toString() {
		return "Equipa [jogo=" + jogo + ", seleccao=" + seleccao + "]\n";
	}

	public int getNumSubstituicoes() {
		return nSubstituicoes;
	}

	public void setSubstituicoes() {
		nSubstituicoes--;
	}

	public void dispensarJogador(Jogador jogador) {
		for (Jogador j : jogador()) {
			if (j == jogador)
				membro().remove(j);
		}
	}

	public void convocarJogador(Membro jogador) {
		membro().add(jogador);
	}

}
